package cn.lizemin.demo.thread.waiting;

/**
 * @author: lizemin
 * @version: 1.0
 */
public class WaitingDemo {
    private static Object obj = new Object();
    //这是一个main方法,是程序的入口
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread() {
            @Override
            public void run() {
                synchronized (obj){
                    System.out.println("获取到锁，调用wait方法，当前线程进入无限等待，等待着别人来唤醒它");
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("唤醒了这个线程，就不再是无限等待了，线程执行完毕");
            }
        };
        t.start();
        //创建一个线程对象唤醒前面的线程
        new Thread(){
            @Override
            public void run() {
                synchronized (obj){
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("获取到锁，执行唤醒操作");
                    obj.notify();
                }


            }
        }.start();
        Thread.sleep(3000);
        //waiting
        System.out.println("查看线程t的状态："+t.getState());



    }

}
